package com.cloud.pole.shop.controller.order;

import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.shop.controller.IndexController;
import com.cloud.pole.shop.model.vo.IndexVo;
import com.cloud.pole.shop.model.vo.goods.GoodsDetailVo;
import com.cloud.pole.shop.model.vo.order.ShopOrderToConfirmVo;
import com.cloud.pole.shop.model.vo.order.ShopOrderToPayVo;
import com.cloud.pole.shop.model.vo.order.ShoppingCartVo;
import com.cloud.pole.shop.service.IndexService;
import com.cloud.pole.shop.service.goods.GoodsService;
import com.cloud.pole.shop.service.order.OrderPayService;
import com.cloud.pole.shop.service.order.OrderService;
import com.cloud.pole.shop.service.order.ShoppingCartService;

import lombok.extern.slf4j.Slf4j;

/*
*2019年2月21日
*cloud-shop
*author:frank-chen
*/
@Controller
@Slf4j
@RequestMapping("/orderPay/front")
public class OrderPayController {

	@Autowired
	private OrderPayService orderPayService;
	
	/**
	 *  获取支付前数据
	 * @param token
	 * @param orderIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getToOrderPay")
    public ApiRestResponse<ShopOrderToPayVo> getToOrderPay(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String orderIds){   	
    	try {
    		orderIds = URLDecoder.decode(orderIds, "UTF-8");
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(orderIds), "orderIds不能为空");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, orderPayService.getToOrderPay(token, orderIds));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	/**
	 * 订单支付
	 * @param token
	 * @param orderIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doOrderPay")
    public ApiRestResponse<String> doOrderPay(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String payId,
    		@RequestParam(defaultValue = "") String payType,
    		@RequestParam(defaultValue = "") String payPwd,
    		@RequestParam(defaultValue = "") String openId,
    		@RequestParam(defaultValue = "") String payMethod,
    		@RequestParam(defaultValue = "") String stageNumId,
    		@RequestParam(defaultValue = "") String stageNum){
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(payId), "payId不能为空");
			ParamException.err(StringUtils.isBlank(payType), "payType不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(payId), "payId错误");
			ParamException.err(!RegexUtils.isPositiveInteger(payType), "payType错误");
			ParamException.err(!RegexUtils.isPositiveInteger(payMethod), "payMethod错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, orderPayService.doOrderPay(token, 
					Long.parseLong(payId), Integer.parseInt(payType), payPwd, openId, Integer.valueOf(payMethod),
					StringUtils.isBlank(stageNumId) ? null : Integer.valueOf(stageNumId), 
					StringUtils.isBlank(stageNum) ? null : Integer.valueOf(stageNum)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	@ResponseBody
	@RequestMapping("/synOrderPayStatus")
    public ApiRestResponse<String> synOrderPayStatus(
    		@RequestParam(defaultValue = "") String joinAppId,
    		@RequestParam(defaultValue = "") String pgOrderSn,
    		@RequestParam(defaultValue = "") String transactionId,
    		@RequestParam(defaultValue = "") String sign,
    		@RequestParam(defaultValue = "") String nonceStr){
    	try {
			ParamException.err(StringUtils.isBlank(joinAppId), "joinAppId不能为空");    		
			ParamException.err(StringUtils.isBlank(pgOrderSn), "pgOrderSn不能为空");
			ParamException.err(StringUtils.isBlank(pgOrderSn), "pgOrderSn不能为空");
			ParamException.err(StringUtils.isBlank(sign), "sign不能为空");
			ParamException.err(StringUtils.isBlank(nonceStr), "nonceStr不能为空");
			
			ParamException.err(!RegexUtils.isPositiveInteger(joinAppId), "joinAppId错误");
			orderPayService.synOrderPayStatus(Integer.parseInt(joinAppId), pgOrderSn, transactionId, nonceStr, sign);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, null);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
}
